package com.ly.mpayment.persist.service.operators;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.Operators;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

@Service
public class OperatorsServiceImpl extends AbstractService<Operators, Serializable> implements IOperatorsService{

	@Override
	public boolean addOperators(Operators op) {
		if(op != null){
			this.saveOrUpdate(op);
			return true;
		}
		return false;
	}

	@Override
	public Operators findById(String id) {
		return this.findUnique("select o from Operators o where  o.operatoruid = ?", id);
	}

	@Override
	public Page<Operators> findPageOperator(Map<String, Object> parameters, int start, int pageSize) {
		return this.findPageByNamed("select o from Operators o where 1 = 1 #[and o.operatorname = :operatorname]# order by o.operatorname ", parameters, start, pageSize);
	}

	@Override
	public List<Operators> findAll() {
		return this.find("select o from Operators o where o.status = 1  order by o.operatorname");
	}

	@Override
	public boolean updateOperator(JSONObject json) {
		Operators operators = this.findById(json.getString("operatoruid"));
		if(operators != null){
			operators.setStatus(json.getString("status"));
			operators.setRemark(json.getString("remark"));
			operators.setOperatorname(json.getString("operatorname"));
			operators.setCode(json.getString("code"));
			return this.updateEntity(operators) != null;
		}
		return false;
	}

	@Override
	public boolean updateStatusOperator(JSONObject json) {
		Operators operators = this.findById(json.getString("operatoruid"));
		if(operators != null){
			operators.setStatus(json.getString("status"));
			return this.updateEntity(operators) != null;
		}
		return false;
	}
}
